package cn.wolfcode.wolf2w.controller;

import cn.wolfcode.wolf2w.annotation.RequireLogin;
import cn.wolfcode.wolf2w.annotation.UserParam;
import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.query.StrategyCommentQuery;
import cn.wolfcode.wolf2w.query.StrategyQuery;
import cn.wolfcode.wolf2w.redis.service.IStrategyStratisVoRedisService;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVo;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.service.IStrategyThemeService;
import cn.wolfcode.wolf2w.util.JsonResult;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("strategies")
public class StrategyController {

    @Autowired
    private IStrategyService strategyService;

    @Autowired
    private IStrategyThemeService strategyThemeService;

    @Autowired
    private IStrategyCommentService strategyCommentService;

    @Autowired
    private IStrategyStratisVoRedisService strategyStratisVoRedisService;

    @GetMapping("/query")
    public Object query(StrategyQuery qo) {
        // 上架
        qo.setState(Strategy.STATE_PUBLISH);
        // page
        Page<Strategy> query = strategyService.query(qo);
        return JsonResult.success(query);
    }

    @GetMapping("/detail")
    public Object detail(String id) {
        return JsonResult.success(strategyService.get(id));
    }

    @GetMapping("/themes")  // 获取所有的主题
    public Object getThemes() {
        return JsonResult.success(strategyThemeService.listAll());
    }

    @RequireLogin
    @PostMapping("/addComment")  // 添加评论
    public Object addComment(StrategyComment strategyComment, @UserParam UserInfo userInfo) {
        // 将用户信息的相同字段值 设置到 评论中
        BeanUtils.copyProperties(userInfo, strategyComment);
        strategyComment.setUserId(userInfo.getId());

        strategyCommentService.addComment(strategyComment);
        return JsonResult.success();
    }

    @GetMapping("/comments")  // 评论分页
    public Object comments(StrategyCommentQuery qo) {
        // page
        Page<StrategyComment> page = strategyCommentService.query(qo);
        return JsonResult.success(page);
    }

    @RequireLogin
    @PostMapping("/commentThumb")  // 评论点赞
    public Object commentThumb(String cid, @UserParam UserInfo userInfo) {
        strategyCommentService.commentThumb(cid, userInfo.getId());
        return JsonResult.success();
    }

    @GetMapping("/statisVo")  // 显示统计的数，阅读量+1
    public Object statisVo(String sid) {
        // vo 统计数据
        StrategyStatisVo vo = strategyService.getstatisVo(sid);
        return JsonResult.success(vo);
    }

    @RequireLogin
    @PostMapping("/favor")  // 收藏
    public Object favor(String sid,@UserParam UserInfo userInfo) {
        boolean ret = strategyStratisVoRedisService.favornumIncrease(sid,userInfo.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("ret",ret);
        return JsonResult.success(map);
    }

    @RequireLogin
    @PostMapping("/strategyThumbup")  // 顶赞
    public Object strategyThumbup(String sid,@UserParam UserInfo userInfo) {
        boolean ret = strategyStratisVoRedisService.thumbupnumIncrease(sid,userInfo.getId());
        Map<String, Object> map = new HashMap<>();
        map.put("ret",ret);
        return JsonResult.success(map);
    }
}